package top.mccsdl.Acbot.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.mccsdl.Acbot.BotApplication;
import top.mccsdl.Acbot.entity.KeyValue;
import top.mccsdl.Acbot.entity.Message;
import top.mccsdl.Acbot.entity.user.Plugins;
import top.mccsdl.Acbot.entity.user.UserInfo;
import top.mccsdl.Acbot.service.UserService;
import top.mccsdl.Acbot.utils.PictureUtil;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

@RestController
@Slf4j
public class FileLookController {

    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public void test() throws MalformedURLException {

    }

    @GetMapping("/show")
    public ResponseEntity<Resource> show() throws MalformedURLException {
        //TODO 检测图片是否有更新
        List<Plugins> plugins = userService.selectShow();
        PictureUtil.createImages(plugins, "img\\", "show.png");
        File file = new File("img\\show.png");
        URL url = file.toURI().toURL();
        UrlResource resource = new UrlResource(url);

        if (!resource.exists() || !resource.isReadable()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }

    @GetMapping("/info/{uuid}/{istmStr}/{key}")
    public ResponseEntity<Resource> info(@PathVariable("uuid") String uuid, @PathVariable("istmStr") String istmStr, @PathVariable("key") String key) throws MalformedURLException {
        String s = BotApplication.map.get(key);
        if (s != null && !"".equals(s)) {
            BotApplication.map.remove(key);
        } else {
            return null;
        }
//        boolean istm = Boolean.parseBoolean(istmStr);
//        System.out.println("是否脱敏"+istm);
        List<KeyValue> keyValues = userService.selectAuthByQQ(uuid, true);
        UserInfo userInfo = userService.selectUserInfoByUuid(uuid);
        if (userInfo == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        PictureUtil.createImagesUserInfo(keyValues, userInfo, "img\\", userInfo.getQq() + ".png");
        File file = new File("img\\" + userInfo.getQq() + ".png");
        URL url = file.toURI().toURL();
        UrlResource resource = new UrlResource(url);

        if (!resource.exists() || !resource.isReadable()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}
